import {Component, OnInit} from '@angular/core';
import {HttpService} from '../../utils/http.service';
import {LOGINOUT,CHECKUSERLOGIN} from '../../const/api';

@Component({
  selector: 'app-header',
  templateUrl: './header.component.html',
  styleUrls: ['./header.component.less']
})
export class HeaderComponent implements OnInit {
  isUserLogin: boolean = true;
  userName: string = '';

  constructor(private HttpService: HttpService) {
  }

  ngOnInit() {
    this.checkUserLogin();
  }

  logout() {
    //向服务器端发请求，请求user/logout.php
    this.HttpService
        .sendRequest(LOGINOUT)
        .subscribe((res: any) => {
          if (res.code == 200) {
            this.isUserLogin = false;
            this.userName = '';
          }
        });
  }

  checkUserLogin(){
    this.HttpService.sendRequest(CHECKUSERLOGIN).subscribe((res:any)=>{
      console.log(res);
      if(res.uid){
        this.isUserLogin = true;
        this.userName = "张三";
      }
      else{
        this.isUserLogin = false;
        this.userName = "";
      }
    })
  }

}
